package unit3.part9;

import java.util.Arrays;

public class ArrayTest {
    // psvm
    public static void main(String[] args) {
        // 一维数组创建
        int a[] = {0, 1, 2,};
        int[] b = {0, 1, 2,};
        int[] c = new int[3];
        c[1] = 1;
        c[2] = 2;
        System.out.println(Arrays.toString(a));
        System.out.println(Arrays.toString(b));
        System.out.println(Arrays.toString(c));
        System.out.println("=============================");

        // 一维数组copy
        int[] d = {0, 1, 2};
        int[] e = d;
        e[0] = 3;
        System.out.println(Arrays.toString(e));
        System.out.println(Arrays.toString(d));
        System.out.println("=============================");

        // 一维数组遍历
        // fori
        for (int i = 0; i < a.length; i++) {// 可以获取数据
            System.out.print(a[i] + " ");
        }
        System.out.println();
        // foreach
        for (int x : a) {// 可以获取数据
            System.out.print(x + " ");
        }
        System.out.println();
        for (int i = 0; i < a.length; i++) {// 可以修改数据
            a[i] = 1;
        }
        System.out.println(Arrays.toString(a));
        for (int x : a) {// 不可以修改数据
            x = 2;
        }
        System.out.println(Arrays.toString(a));
        System.out.println("=============================");

        // 二维数组创建
        int[][] f = {{101, 102,}, {201, 202}};
        int g[][] = {{101, 102,}, {201, 202}};
        int[][] h = new int[2][2];
        h[0][0] = 101;
        h[0][1] = 102;
        h[1][0] = 201;
        h[1][1] = 202;
        System.out.println(Arrays.toString(f));// 无法正确打印
        System.out.println("=============================");

        // 二维数组访问
        for (int i = 0; i < f.length; i++) {
            for (int j = 0; j < f[i].length; j++) {
                System.out.print(f[i][j] + "\t");
            }
            System.out.println();
        }
        for (int[] x : f) {
            for (int y : x) {
                System.out.print(y + "\t");
            }
            System.out.println();
        }
        for (int[] x : f) {
            System.out.println(Arrays.toString(x));
        }
        System.out.println("=============================");

        // 矩阵转置
        // 1 2 3
        // 4 5 6
        int[][] origin = {{1, 2, 3}, {4, 5, 6}};
        // 1 4
        // 2 5
        // 3 6
        int[][] target = new int[3][2];
        for (int i = 0; i < origin.length; i++) {
            for (int j = 0; j < origin[i].length; j++) {
                target[j][i] = origin[i][j];
            }
        }
        for (int[] x : origin) {
            System.out.println(Arrays.toString(x));
        }
        for (int[] y : target) {
            System.out.println(Arrays.toString(y));
        }
    }
}
